package htmodels

import (
	"github.com/astaxie/beego/orm"
	"choujiang/models"
)
func HTDevicesCouponAdd(devicescoupon *models.DevicesCoupon) error {
	o := orm.NewOrm()
	_, err := o.Insert(devicescoupon)
	return err
}

func HTDevicesCouponGetDetail(devicescoupon *models.DevicesCoupon) error {
	o := orm.NewOrm()
	return o.Read(devicescoupon)
}

func HTDevicesCouponGetCount(search string, searchFiled string , CouponStatus int) (int64, error) {
	o := orm.NewOrm()
	m := models.DevicesCoupon{}
	count, err := o.QueryTable(m.TableName()).Filter(m.FN_CouponStatus(), CouponStatus).Filter(searchFiled + "__icontains", search).Count()
	return count, err
}

func HTDevicesCouponGetList(devicescoupons *[]models.DevicesCoupon, fields []string , CouponStatus int, offset int64, limit int, search string, searchFiled string, orderby string) error {
	o := orm.NewOrm()
	m := models.DevicesCoupon{}
	_, err := o.QueryTable(m.TableName()).Filter(m.FN_CouponStatus(), CouponStatus).Filter(searchFiled + "__icontains", search).OrderBy(orderby).Offset(offset).Limit(limit).All(devicescoupons, fields...)
	return err
}

func HTDevicesCouponDelete(Ids []int64) (int64, error) {
	o := orm.NewOrm()
	m := models.DevicesCoupon{}
	count, err := o.QueryTable(m.TableName()).Filter("Id" + "__in", Ids).Delete()
	return count, err
}
func HTDevicesCouponStatusChange(Ids []int64, status int) (int64, error) {
	o := orm.NewOrm()
	m := models.DevicesCoupon{}
	count, err := o.QueryTable(m.TableName()).Filter("Id" + "__in", Ids).Update(orm.Params{
		m.FN_CouponStatus(): status,
	})
	return count, err
}
func HTDevicesCouponUpdate(devicescoupon *models.DevicesCoupon) error {
	o := orm.NewOrm()
	_, err := o.Update(devicescoupon)
	return err
}